home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character - strtok trick
- Date: Sat, 06 Apr 96 15:11:45 GMT
- Organization: none
- Message-ID: <828803505snz@genesis.demon.co.uk>
- References: <31616F63.481D@lava.weeg.uiowa.edu> <828493319snz@genesis.demon.co.uk> <828542474.7672@tertio.demon.co.uk> <4ju9m0$nq9@belle.bork.com> <4k1jmd$ifr@sun001.spd.dsccc.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4k1jmd$ifr@sun001.spd.dsccc.com>
- jmccarty@sun1307.spd.dsccc.com "Mike McCarty" writes:
-
- >I usually do this (from memory only, so argument order etc. may be off):
- >
- > boolean readline(char *buffer,size_t max_size,FILE *stream) {
- > boolean result;
- > size_t place;
- >
- > if (fgets(buffer,max_size,stream) == NULL)
- > result = false;
- > else {
- > place = strlen(buffer)-1;
- > if (place >= 0 && buffer[place] == '\n')
-
- Since size_t is an unsigned type place >= 0 will always be true.
-
- > buffer[place] = '\0';
- > result = true;
- > }
- > return result;
- > }
-
- If you maintain the form of fgets() you could use this as a drop-in
- replacement. I see no advantage to introducing a boolean 'type'.
-
- char *readline(char *buffer, int max_size, FILE *stream)
- {
-
- char *const result = fgets(buffer, max_size, stream);
-
- if (result != NULL) {
- int place = strlen(result);
-
- if (place != 0 && result[--place] == '\n')
- result[place] = '\0';
- }
-
- return result;
- }
-
- I used int rather than size_t since that is the limit on size imposed by
- fgets(), and the 2nd argument int makes clear that the function doesn't
- work with a buffer size greater than INT_MAX.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-